package com.ruoyi.application.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.application.domain.Application;
import com.ruoyi.application.domain.ResultVo;
import com.ruoyi.application.service.IApplicationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 申请列表Controller
 *
 * @author ruoyi
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/application/application")
public class ApplicationController extends BaseController {
    @Autowired
    private IApplicationService applicationService;


    /**
     * 查询申请列表列表
     */
    @PreAuthorize("@ss.hasPermi('application:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(Application application) {
        startPage();
        List<Application> list = applicationService.selectApplicationList(application);
//        去除掉已经审核的数据
        list.removeIf(a -> ObjectUtils.isNotNull(a.getExamineStatus()));
        return getDataTable(list);
    }

    /**
     * 导出申请列表列表
     */
    @PreAuthorize("@ss.hasPermi('application:application:export')")
    @Log(title = "申请列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Application application) {
        List<Application> list = applicationService.selectApplicationList(application);
        ExcelUtil<Application> util = new ExcelUtil<Application>(Application.class);
        util.exportExcel(response, list, "申请列表数据");
    }

    /**
     * 获取申请列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('application:application:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(applicationService.selectApplicationById(id));
    }

    /**
     * 新增申请列表
     */
//    @PreAuthorize("@ss.hasPermi('application:application:add')")
//    @Log(title = "申请列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(HttpServletRequest request, @RequestBody Application application) throws Exception {
        return toAjax(applicationService.insertApplication(request, application));
    }

    /**
     * 修改申请列表
     */
    @PreAuthorize("@ss.hasPermi('application:application:edit')")
    @Log(title = "申请列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Application application) {
        return toAjax(applicationService.updateApplication(application));
    }

    /**
     * 删除申请列表
     */
    @PreAuthorize("@ss.hasPermi('application:application:remove')")
    @Log(title = "申请列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(applicationService.deleteApplicationByIds(ids));
    }

    /**
     * 申请通过
     */
    @PreAuthorize("@ss.hasPermi('application:application:pass')")
    @Log(title = "申请列表", businessType = BusinessType.UPDATE)
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody Application application) {
        return toAjax(applicationService.pass(application));
    }

    /**
     * 申请拒绝
     */
    @PreAuthorize("@ss.hasPermi('application:application:refuse')")
    @Log(title = "申请列表", businessType = BusinessType.UPDATE)
    @PostMapping("/refuse")
    public AjaxResult refuse(@RequestBody Application application) {
        return toAjax(applicationService.refuse(application));
    }


    /**
     * 获取申请数
     */
    @PreAuthorize("@ss.hasPermi('application:application:count')")
    @GetMapping("/count")
    public AjaxResult count() {
        List<Application> applications = applicationService.selectApplicationList(new Application());
        applications.removeIf(ObjectUtils::isNotNull);
        return AjaxResult.success(applications.size());
    }

    /**
     * 查询申请状态
     */
    @GetMapping("/getResult")
    public ResultVo getResult(Application application) {
        return applicationService.getResult(application);
    }
}